package com.ishanshan.gateway.config;

import com.ishanshan.cache.SbaoRedissonFactoryBean;
import org.redisson.codec.KryoCodec;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 */
@Configuration
@ConfigurationProperties(prefix="redission")
public class RedissionConfiguration {
	
	private String master;
	
	private String slaves;
	
	private String password;
	
	private int timeout;

	private KryoCodec kryoCodec = new KryoCodec();

	@Bean
	public SbaoRedissonFactoryBean redissonFactoryBean(){
		SbaoRedissonFactoryBean rfBean = new SbaoRedissonFactoryBean(){
			@Override
			public Config getConfig() {
				Config cfg = new Config();
				cfg.setCodec(kryoCodec);
				MasterSlaveServersConfig mcfg = cfg.useMasterSlaveServers()
						.setMasterAddress(master)
						.setPassword(password)
						.setDatabase(10)
						.setTimeout(timeout);
				if(slaves!=null){
					String[] slavesArr = slaves.split(",");
					for(String slave : slavesArr){
						if(!"".equals(slave.trim())){
							mcfg.addSlaveAddress(slave);
						}
					}
				}

				return cfg;
			}
		};
		return rfBean;
	}

	public String getMaster() {
		return master;
	}

	public void setMaster(String master) {
		this.master = master;
	}

	public String getSlaves() {
		return slaves;
	}

	public void setSlaves(String slaves) {
		this.slaves = slaves;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}
	
	
	

}
